package com.example.onlinemusicserver.controller;

import com.example.onlinemusicserver.mapper.LoveMusicMapper;
import com.example.onlinemusicserver.model.Music;
import com.example.onlinemusicserver.model.User;
import com.example.onlinemusicserver.tools.Constant;
import com.example.onlinemusicserver.tools.ResponseBodyMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Resource
    private LoveMusicMapper loveMusicMapper;

    /**
     * 收藏音乐
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> likemusic(@RequestParam String id, HttpServletRequest request){
        System.out.println("musicId:"+id);
        int musicId=Integer.parseInt(id);

        // 1. 检查是否登录了
        HttpSession session=request.getSession(false);
        if (session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            System.out.println("当前用户未登录");
            return new ResponseBodyMessage<>(-1,"请先登录再操作",false);
        }

        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getId();
        System.out.println("userId:"+userId);

        Music findMusic=loveMusicMapper.findMusic(musicId);
        if (findMusic==null){
            return new ResponseBodyMessage<>(-1,"这首音乐不存在",false);
        }else {
            Music music=loveMusicMapper.findLoveMusic(userId,musicId);
            if (music!=null){
                // 之前收藏过，不能进行收藏，todo:加一个取消收藏的功能

                // 取消收藏
//            boolean ret=loveMusicMapper.deleteLoveMusic(userId,musicId);
//            if (ret==true){
//                return new ResponseBodyMessage<>(0,"取消收藏成功",true);
//            }else {
//                return new ResponseBodyMessage<>(-1,"取消收藏失败",true);
//            }

                return new ResponseBodyMessage<>(-1,"请勿重复收藏",false);
            }
            boolean effect=loveMusicMapper.insertLoveMusic(userId,musicId);
            if (effect){
                return new ResponseBodyMessage<>(0,"收藏音乐成功",true);
            }else {
                return new ResponseBodyMessage<>(-1,"收藏音乐失败",false);
            }
        }
    }

    /*
       查询收藏的音乐
     */
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,HttpServletRequest request){
        // 1. 检查是否登录了
        HttpSession session=request.getSession(false);
        if (session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            System.out.println("当前用户未登录");
            return new ResponseBodyMessage<List<Music>>(-1,"请先登录再操作",null);
        }

        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getId();
        System.out.println("userId:"+userId);

        List<Music> musicList=null;
        if (musicName==null){
            musicList=loveMusicMapper.findMusicByUserId(userId);
        }else {
            musicList=loveMusicMapper.findMusicByUserIdAndMusciName(musicName,userId);
        }
        if (musicList.size()==0){
            return new ResponseBodyMessage<>(-1,"当前没有你所要查询的音乐，请重新输入",null);
        }
        return new ResponseBodyMessage<>(0,"查询音乐信息成功，查询到的数目是："+musicList.size(),musicList);
    }

    /**
     * 移除收藏的音乐
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id,HttpServletRequest request){
        int musicId=Integer.parseInt(id);
        // 1. 检查是否登录了
        HttpSession session=request.getSession(false);
        if (session==null || session.getAttribute(Constant.USERINFO_SESSION_KEY)==null){
            System.out.println("当前用户未登录");
            return new ResponseBodyMessage<>(-1,"请先登录再操作",null);
        }

        User user=(User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId=user.getId();
        System.out.println("userId:"+userId);

        int ret=loveMusicMapper.deleteLoveMusic(userId,musicId);
        if (ret==1){
            return new ResponseBodyMessage<>(0,"移除收藏音乐成功！",true);
        }else {
            return new ResponseBodyMessage<>(-1,"移除收藏音乐失败",false);
        }

    }
}
